<?php

/**
 * @author thihien
 * @copyright 2011
 */
$sql='SELECT count(id) as count FROM `tbl_san_pham` ';
$kq=tep_db_query($sql);
$item1=tep_db_fetch_array($kq);
//bat dau viet phan trang
$display=12;// so san pham xuat hien tren mot trang
$record=$item1['count'];//tong so san pham
if($record>$display){
    $page=ceil($record/$display);
}
else{
    $page=1;
}
$start=isset($_GET['start']) && (int)$_GET['start']?$_GET['start']:0;
$sql4='select * from tbl_san_pham limit '.$start.', '.$display.'';
$kq4=tep_db_query($sql4);
$content='';
$content.='
     <div id="main-content">
     <div id="sp_noibat">';
          
            
        
        $content.='<div class="tieudetrang"><h2>sản phẩm</h2></div>
        <div class="sp_noibat">';
            
                $sql='select * from tbl_san_pham ';
                $kq=tep_db_query($sql);
                while($item=tep_db_fetch_array($kq)){
                $content.='<div  class="hinhanhsanpham">';
                if($item['hinh_anh']!=null){
                     $content.='<div class="h_a"><a href="'.DOMAIN_NAME.'/get/page/sanpham/masp/'.$item['id'].'/madanhmucsp/'.$item['ma_danh_muc'].'/mode/detail"><img src="'.PRODUCT_IMAGE_URL.$item['hinh_anh'].'" width="218" height="177" border="1px" /></a></div>';
                   
                }
                    $content.='<div class="tensp">
                        <p><a href="?page=sanpham&masp='.$item['id'].'&madanhmucsp='.$item['ma_danh_muc'].'&mode=detail">'.$item['ten_san_pham'].'</a></p>
                        <p><a href="?page=sanpham&masp='.$item['id'].'&madanhmucsp='.$item['ma_danh_muc'].'&mode=detail">Giá : '.$item['gia'].' VNĐ</a></p>                                        
                        <p><a href="?page=sanpham&masp='.$item['id'].'&madanhmucsp='.$item['ma_danh_muc'].'&mode=detail">NSX : '.$item['noi_san_xuat'].'</a><p>
                        <p style="padding-top: 5px;"><a href="javascript:buyProduct('.$item['id'].');"><img src="http://localhost/chuotlenshop/images/giohang.jpg" /></a><p>
                    </div>
                   </div>';
                   
                }
            
       $content.=' </div>
    

</div>';
        
$content.='
        <ul class="phantrang">';
         $content.='Trang:';
            if($page>1){
                $next=$start+$display;
                $prev=$start-$display;
                $current=($start/$display)+1;
                //hien thi phan trang prev
               
                if($current!=1){
                   $content.='<li><a href="?page=sanpham&start='.$prev.'">Previous</a></li>'; 
                }
                 //hien thi trang
                 for($i=1;$i<=$page;$i++){
                   if($current!=$i){
                        $content.='<li><a href="?page=sanpham&start='.$display*($i-1).'">'.$i.'</a></li>'; 
                    } 
                    else
                    {
                       $content.='<li class="current"><a href="?page=sanpham&start='.$display*($i-1).'">'.$i.'</a></li>'; 
                    }
                     
                 }
                //hien thi next
                if($current!=$page){
                    $content.='<li><a href="?page=sanpham&start='.$next.'">Next</a></li>';  
                }
            }
            else{
                $content.='<li class="current" ><a href="?page=sanpham&start=0">'.$page.'</a></li>';  
            }
       $content.=' </ul>
  
</div> 
';

if(isset($_GET['mode']) && $_GET['mode']=='detail'){

$madanhmuc=$_GET['madanhmucsp'];
$masp=$_GET['masp'];
$sql='select * from tbl_san_pham where id='.$masp;
$kq=tep_db_query($sql);

$content='
     <div id="main-content">
     <div id="sp_noibat">';
          
            
        
        $content.='<div class="tieudetrang"><h2>sản phẩm</h2></div>
        <div class="sp_noibat">';
            
                $item=tep_db_fetch_array($kq);
                $content.='<div  class="hinhanhct">';
                if($item['hinh_anh']!=null){
                     $content.='<div class="hinhanh_ct"><a href=""><img src="'.PRODUCT_IMAGE_URL.$item['hinh_anh'].'" width="320" border="1px" /></a></div>';
                  
                }
                     
                     $content.='<div class="tensp1">
                     <table class="tb_chitiet" cellpadding="5" cellspacing="5">
                        <tr>
                            <td >
                                Tên Sản Phẩm
                              
                            </td>
                            <td style="width:190px;">
                                '.$item['ten_san_pham'].'
                               
                            </td>
                        </tr>
                        <tr>
                            <td>
                                Giá
                              
                            </td>
                            <td style="width:190px;">
                                '.$item['gia'].' VNĐ
                               
                            </td>
                        </tr>
                        <tr>
                            <td>
                               NSX
                              
                            </td>
                            <td style="width:190px;">
                                '.$item['noi_san_xuat'].'
                               
                            </td>
                        </tr>
                          <tr>
                            <td>
                               Mô Tả Sản Phẩm
                              
                            </td>
                            <td style="width:190px;">
                                '.$item['mo_ta_san_pham'].'
                               
                            </td>
                        </tr>
                        
                     </table>
                      <p style="padding-top: 15px;text-align:right;"><a href="javascript:buyProduct('.$item['id'].');"><img src="http://localhost/chuotlenshop/images/giohang.jpg" /></a><p>
                   
                     </div>
                   </div>
                  <div class="clear"></div> ';
   
$sql2='SELECT count(id) as count FROM `tbl_san_pham` where id!='.$masp.' and ma_danh_muc='.$madanhmuc ;
$kq2=tep_db_query($sql2);
$item2=tep_db_fetch_array($kq2);
//bat dau viet phan trang
$display=6;// so san pham xuat hien tren mot trang
$record=$item2['count'];//tong so san pham
if($record>$display){
    $page=ceil($record/$display);
    
}
else{
    $page=1;
}
$start=isset($_GET['start']) && (int)$_GET['start']?$_GET['start']:0;
$sql4='select * from tbl_san_pham where ma_danh_muc='.$madanhmuc.' and id!='.$masp.' limit '.$start.', '.$display.'';
$kq4=tep_db_query($sql4);
$content.='
       
            <div class="tieudetrang"><h2>sản phẩm cùng loại</h2></div>
            <div class="sp_noibat">';
            
            while($item1=tep_db_fetch_array($kq4)){
            $content.='
             
              <div  class="hinhanhsanpham">';
              if($item1['hinh_anh']!=null){
                 $content.='<div class="h_a"><a href="?page=sanpham&masp='.$item1['id'].'&madanhmucsp='.$item1['ma_danh_muc'].'&mode=detail"><img src="admin/upload/'.$item1['hinh_anh'].'" width="218" height="177" border="1px" /></a></div>';
                  
              }
                     $content.='<div class="tensp">
                        <p><a href="?page=sanpham&masp='.$item1['id'].'&madanhmucsp='.$item1['ma_danh_muc'].'&mode=detail">'.$item1['ten_san_pham'].'</a></p>
                        <p><a href="?page=sanpham&masp='.$item1['id'].'&madanhmucsp='.$item1['ma_danh_muc'].'&mode=detail">Giá : '.$item1['gia'].' VNĐ</a></p>                                        
                        <p><a href="?page=sanpham&masp='.$item1['id'].'&madanhmucsp='.$item1['ma_danh_muc'].'&mode=detail">NSX : '.$item1['noi_san_xuat'].'</a><p>
                        <p style="padding-top: 5px;"><a href="javascript:buyProduct('.$item1['id'].');"><img src="http://localhost/chuotlenshop/images/giohang.jpg" /></a><p>
                    </div>
              </div>';
                
        }

        
       $content.='</div> 
         <ul class="phantrang">';
         $content.='<span style="color: #7d171d;font-weight:bold;">Trang :</span>';
            if($page>1){
                $next=$start+$display;
                $prev=$start-$display;
                $current=($start/$display)+1;
                //hien thi phan trang prev
               
                if($current!=1){
                   $content.='<li><a href="?page=sanpham&masp='.$masp.'&madanhmucsp='.$madanhmuc.'&start='.$prev.'&mode=detail">Previous</a></li>'; 
                }
                 //hien thi trang
                 for($i=1;$i<=$page;$i++){
                   if($current!=$i){
                        $content.='<li><a href="?page=sanpham&masp='.$masp.'&madanhmucsp='.$madanhmuc.'&start='.$display*($i-1).'&mode=detail">'.$i.'</a></li>'; 
                    } 
                    else
                    {
                       $content.='<li class="current"><a href="?page=sanpham&masp='.$masp.'&madanhmucsp='.$madanhmuc.'&start='.$display*($i-1).'&mode=detail">'.$i.'</a></li>'; 
                    }
                     
                 }
                //hien thi next
                if($current!=$page){
                    $content.='<li><a href="?page=sanpham&masp='.$masp.'&madanhmucsp='.$madanhmuc.'&start='.$next.'&mode=detail">Next</a></li>';  
                }
            }
            else{
                $content.='<li class="current" ><a href="?page=sanpham&masp='.$masp.'&madanhmucsp='.$madanhmuc.'&start=0&mode=detail">'.$page.'</a></li>';  
            }
       $content.=' </ul>   
        </div>
       
    </div> </div>
';
}


echo $content;

?>
